
#include "StdAfx.h"
#include "Start_End_Time.h"
#include "LogService.h"

Start_End_Time::Start_End_Time(){
	type_ = 0;
	tick_.reset( DEF_MAX_TICK );
}

Start_End_Time::~Start_End_Time(){
}

void Start_End_Time::set_type( int type ){
	type_ = type;
}

int Start_End_Time::get_type(){
	return type_;
}

bool Start_End_Time::is_set_start(){
	if( tick_.get_tick( DEF_START_IDX ) )
		return true;
	return false;
}

bool Start_End_Time::is_set_end(){
	if( tick_.get_tick( DEF_END_IDX ) )
		return true;
	return false;
}

void Start_End_Time::set_start( int start, int end ){
	LOG_FUNC( LOG_SERVICE( "instance" )->Log( "type : %u - start : %u - end : %u", \
		type_, start, end ) );
	
	int current = (int)time( 0 );
	int start_tick = current + ( start * 60 );
	int end_tick = 0;

	if( end )
		end_tick = start_tick + ( end * 60 );
	
	tick_.store_sec( DEF_NFY_IDX, current );
	tick_.store_sec( DEF_START_IDX, start_tick );
	tick_.store_sec( DEF_END_IDX, end_tick );
}

void Start_End_Time::set_end( int end ){
	if(! end )
		return;

	int current = (int)time( 0 );
	int end_tick = current + ( end * 60 );

	tick_.store_sec( DEF_NFY_IDX, current );	
	tick_.store_sec( DEF_END_IDX, end_tick );
}

int Start_End_Time::get_start( int& min, int& sec ){		
	return get( min, sec, DEF_START_IDX );
}

int Start_End_Time::get_end( int& min, int& sec ){
	return get( min, sec, DEF_END_IDX );
}

int Start_End_Time::get( int& min, int& sec, int start_or_end ){
	min = 0;
	sec = 0;

	int start = tick_.get_sec( start_or_end );
	if(! start ){
		LOG_INSTANCE( LOG_SERVICE( "instance" )->Log( "err Start is zero" ) );
		return DEF_NO;
	}
		
	int current = static_cast< int >( time( 0 ) );
	if(! current )
		return DEF_NO;

	if( current >= start ){
		tick_.store_sec( start_or_end, 0 );
		return DEF_NOW;
	}	

	int remain = start - current;	
	if( ( remain >= 60 ) ){
		if(! tick_.is_elapsed_sec( DEF_NFY_IDX, 40, current ) )
			return DEF_NO;	
		
		if( ( remain % 60 ) )
			return DEF_NO;

		remain = remain / 60;
		tick_.store_sec( DEF_NFY_IDX, current );
		
		min = remain;
		return DEF_MIN;
	}	
	
	if(! tick_.is_elapsed_sec( DEF_NFY_IDX, 3, current ) )
		return DEF_NO;
	
	if( ( remain % 10 ) )
		return DEF_NO;

	sec = remain;
	tick_.store_sec( DEF_NFY_IDX, current );
	
	return DEF_SEC;	
}
